package org.xsk.algorithm.sort;

import org.xsk.exception.BizException;

/**
 * 比较次数：n^2/2
 * 交换次数：n
 * 
 */
public class SelectSort {

    /**
     * @param arr 数组
     */
    void selectSort(int[] arr){
        if(arr == null){
            throw new BizException();
        }

        if (arr.length < 2) return;

        for (int i = 0; i < arr.length -1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                minIndex = arr[j] < arr[minIndex] ? j : minIndex;
            }
            swap(i, minIndex, arr);
        }
    }
    void swap(int currentIndex,int minIndex,int[] arr){
        if (currentIndex == minIndex) return;

        int temp = arr[currentIndex];
        arr[currentIndex] = arr[minIndex];
        arr[minIndex] = temp;
    }
}
